package cn.wolfcode.mongo.controller;

import cn.wolfcode.mongo.domain.User;
import cn.wolfcode.mongo.service.IUserService;
import cn.wolfcode.mongo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService userService;

    @GetMapping("list")
    public Object list(){
        return userService.list();
    }

    @PostMapping("saveOrUpdate")
    public Object saveOrUpdate(User user){
        System.out.println(user.getName());
        if(!StringUtils.hasLength(user.getId())){
            userService.save(user);
        }else {
            userService.update(user);
        }
         return new JsonResult();
    }


    @GetMapping("get")
    public Object get(String id){
        return userService.get(id);
    }

    @DeleteMapping("delete")
    public Object delete(String id){
        userService.delete(id);
        return new JsonResult();
    }
}
